import 'dart:async';
import 'package:event_bus/event_bus.dart';

class EventBusUtil{

  static EventBus? _eventBus;

  //获取单例
  static EventBus getInstance(){
    if(_eventBus==null){
      _eventBus=EventBus();
    }
    return _eventBus!;
  }

  //返回某事件的订阅者
  static StreamSubscription<T> listen<T extends Event>(Function(T event) onData) {
    if(_eventBus==null){
      _eventBus=EventBus();
    }
    //内部流属于广播模式，可以有多个订阅者
    return _eventBus!.on<T>().listen(onData);
  }

  //发送事件
  static void fire<T extends Event>(T e) {
    if(_eventBus==null){
      _eventBus=EventBus();
    }
    _eventBus!.fire(e);
  }
}

abstract class Event {}

/// 做饭信息流缓存
class CacheCookEvent extends Event{
  static int cacheCookIn=1;
  final int status;
  CacheCookEvent(this.status);
}

/// 笔记信息流缓存
class CacheNoteEvent extends Event{
  static int cacheNoteIn=1;
  final int status;
  CacheNoteEvent(this.status);
}

/// 课程信息流缓存
class CacheClassEvent extends Event{
  static int cacheClassIn=1;
  final int status;
  CacheClassEvent(this.status);
}

/// 登录
class LoginEvent extends Event{
  static int logIn=1;
  static int logOut=1;
  final int status;
  LoginEvent(this.status);
}